﻿using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using SmartSiteManagementSystem.IServices;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using SmartSiteManagementSystem.Models.DTO;
using SmartSiteManagementSystem.Models.ProjectManage;
using SmartSiteManagementSystem.Models.User;
using SmartSiteManagementSystem.AdminWeb.Filter;

namespace SmartSiteManagementSystem.AdminWeb.Controllers
{
    public class ProjectManageController :AdminBaseControllers
    {
        private readonly ILogger<ProjectManageController> _logger;
        //注入后台服务
        private readonly IAdminService _adminService;
        private readonly IProjectManageService _projectManageService;
        private readonly IUserService _userService;
        public ProjectManageController(ILogger<ProjectManageController> logger
            ,IAdminService adminService
            , IProjectManageService projectManageService
            , IUserService userService
            ) : base(adminService)
        {
            _logger = logger;
            _adminService = adminService;
            _userService = userService;
            _projectManageService = projectManageService;
           

        }

        #region ProjectInfo
        public IActionResult ProjectIndex()
        {
            return View();
        }

        /// <summary>
        /// 获取列表
        /// </summary>
        /// <param name="searchKey"></param>
        /// <param name="page"></param>
        /// <param name="rows"></param>
        public JsonResult GetProjectList(string searchKey, int page = 0, int rows = 50)
        {
            if (page < 1)
            {
                page = 1;
            }
            page = (page - 1) * rows;

            Hashtable param = new Hashtable();
            param.Add("is_delete", 0);//正常 0 false
            int count = _projectManageService.GetProjectInfoListCount(param);
            IList<ProjectInfo> list = _projectManageService.GetProjectInfoList(param, "", page, rows);
            var data = new
            {
                total = count,
                rows = list
            };
            return Json(data);
        }



        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="projectId"></param>
        public JsonResult ajaxdeleteProject(int projectId)
        {
            ResultInfo result = new ResultInfo();
            result.IsSuccess = false;
            result.Code = 200;
            result.Msg = "操作失败!";
            if (projectId > 0)
            {
                ProjectInfo info = _projectManageService.GetProjectInfoById(projectId);
                if (info != null)
                {
                    info.IsDelete = true;
                    result.IsSuccess = _projectManageService.UpdateProjectInfo(info);
                }
            }
            if (result.IsSuccess)
            {
                result.Msg = "操作成功!";
            }
            return Json(result);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult AddOrEditProject(int id = 0)
        {
            ProjectInfo info = new ProjectInfo();
            if (id > 0)
            {
                info = _projectManageService.GetProjectInfoById(id);
            }
            else
            {
                info.StartTime = DateTime.Now;
                info.EndTime = DateTime.Now;
            }
            ViewBag.projectInfo = info;

            return View();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="ProjectId"></param>
        /// <param name="SetName"></param>
        /// <param name="Descript"></param>
        public JsonResult ajaxAddOrEditProject(int ProjectId, string ProjectName, string StartTime, string EndTime)
        {
            ProjectInfo info = null;
            ResultInfo result = new ResultInfo();
            result.IsSuccess = false;
            result.Code = 200;
            result.Msg = "操作失败!";

            bool success = false;
            int code = -1;
            if (ProjectId > 0)
            {
                info = _projectManageService.GetProjectInfoById(ProjectId);
            }
            else
            {
                info = new ProjectInfo();
            }
            info.IsDelete = false;
            info.ProjectName = ProjectName;
            info.StartTime = Convert.ToDateTime(StartTime);
            info.EndTime = Convert.ToDateTime(EndTime);
            info.CreateTime = DateTime.Now;
            if (ProjectId > 0)
            {
                info.UpdateTime = DateTime.Now;
                success = _projectManageService.UpdateProjectInfo(info);
                if (success)
                {
                    code = 1;
                }
            }
            else
            {
                ProjectId = _projectManageService.InsertProjectInfo(info);
                success = ProjectId > 0 ? true : false;
                if (success)
                {
                    code = 1;
                }
            }
            if (code == 1)
            {
                result.IsSuccess = true;
                result.Msg = "操作成功!";
            }
            result.Code = code;
            return Json(result);
        }


        #endregion


        #region DayProjectTypeInfo
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public IActionResult DayProjectTypeIndex()
        {
            return View();
        }


        /// <summary>
        /// 获取列表
        /// </summary>
        /// <param name="searchKey"></param>
        /// <param name="page"></param>
        /// <param name="rows"></param>
        public JsonResult GetDayProjectTypeList(string searchKey, int page = 0, int rows = 50)
        {
            if (page < 1)
            {
                page = 1;
            }
            page = (page - 1) * rows;

            Hashtable param = new Hashtable();
            param.Add("is_delete", 0);//正常 0 false
            int count = _projectManageService.GetDayProjectTypeInfoListCount(param);
            IList<DayProjectTypeInfo> list = _projectManageService.GetDayProjectTypeInfoList(param, "", page, rows);
            var data = new
            {
                total = count,
                rows = list
            };
            return Json(data);
        }



        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="dayProjectTypeId"></param>
        public JsonResult ajaxdeleteDayProjectType(int dayProjectTypeId)
        {
            ResultInfo result = new ResultInfo();
            result.IsSuccess = false;
            result.Code = 200;
            result.Msg = "操作失败!";
            if (dayProjectTypeId > 0)
            {
                DayProjectTypeInfo info = _projectManageService.GetDayProjectTypeInfoById(dayProjectTypeId);
                if (info != null)
                {
                    info.IsDelete = true;
                    result.IsSuccess = _projectManageService.UpdateDayProjectTypeInfo(info);
                }
            }
            if (result.IsSuccess)
            {
                result.Msg = "操作成功!";
            }
            return Json(result);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult AddOrEditDayProjectType(int id = 0)
        {
            DayProjectTypeInfo info = new DayProjectTypeInfo();
            if (id > 0)
            {
                info = _projectManageService.GetDayProjectTypeInfoById(id);
            }
            ViewBag.dayProjectTypeInfo = info;

            return View();
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="DayProjectTypeId"></param>
        /// <param name="DayProjectTypeName"></param>
        public JsonResult ajaxAddOrEditDayProjectType(int DayProjectTypeId, string DayProjectTypeName)
        {
            DayProjectTypeInfo info = null;
            ResultInfo result = new ResultInfo();
            result.IsSuccess = false;
            result.Code = 200;
            result.Msg = "操作失败!";
            bool success = false;
            int code = -1;
            if (DayProjectTypeId > 0)
            {
                info = _projectManageService.GetDayProjectTypeInfoById(DayProjectTypeId);
            }
            else
            {
                info = new DayProjectTypeInfo();
            }
            info.DayProjectTypeName = DayProjectTypeName;
            info.IsDelete = false;
            info.CreateTime = DateTime.Now;
            if (DayProjectTypeId > 0)
            {
                success = _projectManageService.UpdateDayProjectTypeInfo(info);
                if (success)
                {
                    code = 1;
                }
            }
            else
            {
                DayProjectTypeId = _projectManageService.InsertDayProjectTypeInfo(info);
                success = DayProjectTypeId > 0 ? true : false;
                if (success)
                {
                    code = 1;
                }
            }
            if (code == 1)
            {
                result.IsSuccess = true;
                result.Msg = "操作成功!";
            }
            result.Code = code;
            return Json(result);
        }



        #endregion



        #region DayProjectInfo

        public IActionResult DayProjectIndex(int projectId)
        {
            ViewBag.projectId = projectId;
            Hashtable uparam = new Hashtable();
            uparam.Add("is_delete", 0);//正常 0 false

            IList<UserInfo> userList = _userService.GetUserInfoList(uparam, "", 0, 500);

            ViewBag.userList = userList;
            return View();
        }



        /// <summary>
        /// 获取列表
        /// </summary>
        /// <param name="projectId"></param>
        /// <param name="searchKey"></param>
        /// <param name="page"></param>
        /// <param name="rows"></param>
        public JsonResult GetDayProjectList(int projectId, string searchKey, int page = 0, int rows = 50)
        {


            ProjectInfo info = _projectManageService.GetProjectInfoById(projectId);


            if (page < 1)
            {
                page = 1;
            }
            page = (page - 1) * rows;

            Hashtable param = new Hashtable();
            param.Add("project_id", projectId);//正常 0 false
            param.Add("is_delete", 0);//正常 0 false
            int count = _projectManageService.GetDayProjectInfoListCount(param);
            IList<DayProjectInfo> list = _projectManageService.GetDayProjectInfoList(param, "", page, rows);
            foreach(DayProjectInfo item in list)
            {
                if(item.DayProjectType!=0)
                {
                    DayProjectTypeInfo dayProjectTypeInfo = _projectManageService.GetDayProjectTypeInfoById(item.DayProjectType);
                    if (dayProjectTypeInfo != null)
                    {
                        item.DayProjectTypeName = dayProjectTypeInfo.DayProjectTypeName;
                        item.StatusTypeStr = GetStatusTypeStr(item.StatusType);
                       if(item.StatusType>=5 && item.UserId>0)
                        {
                            UserInfo user = _userService.GetUserInfoById(item.UserId);
                            if(user!=null)
                            {
                                item.UserStr = user.ShowName;
                            }
                            
                        }
                        else
                        {
                            item.UserStr = "--";
                        }

                    }
                }
            }



            var data = new
            {
                total = count,
                rows = list
            };
            return Json(data);

        }
        /// <summary>
        /// 0  任务未下发
        /// </summary>
        /// <param name="statusType"></param>
        /// <returns></returns>
        private string GetStatusTypeStr(int statusType)
        {
            string back = _projectManageService.GetStatusTypeStr(statusType);
            return back;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="projectId"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult AddOrEditDayProject(int projectId, int id = 0)
        {

            Hashtable param = new Hashtable();
            param.Add("is_delete", 0);//正常 0 false
            IList<DayProjectTypeInfo> dayProjectTypeInfoList = _projectManageService.GetDayProjectTypeInfoList(param, "", 0, 500);

            DayProjectInfo info = new DayProjectInfo();
            if (id > 0)
            {
                info = _projectManageService.GetDayProjectInfoById(id);
             
            }
            else
            {
                info.StartTime = DateTime.Now;
                info.EndTime = DateTime.Now;
            }
            ViewBag.projectId = projectId;
            ViewBag.dayProjectInfo = info;
            ViewBag.dayProjectTypeInfoList = dayProjectTypeInfoList;

            return View();
        }



        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="dayProjectId"></param>
        public JsonResult ajaxdeleteDayProject(int dayProjectId)
        {
            ResultInfo result = new ResultInfo();
            result.IsSuccess = false;
            result.Code = 200;
            result.Msg = "操作失败!";
            if (dayProjectId > 0)
            {
                DayProjectInfo info = _projectManageService.GetDayProjectInfoById(dayProjectId);
                if (info != null)
                {
                    info.IsDelete = true;
                    result.IsSuccess = _projectManageService.UpdateDayProjectInfo(info);
                }
            }
            if (result.IsSuccess)
            {
                result.Msg = "操作成功!";
            }
            return Json(result);
        }

    
        /// <summary>
        /// 
        /// </summary>
        /// <param name="ProjectId"></param>
        /// <param name="DayProjectId"></param>
        /// <param name="DayProjectName"></param>
        /// <param name="DayProjectType"></param>
        /// <param name="StartTime"></param>
        /// <param name="EndTime"></param>
        public JsonResult ajaxAddOrEditDayProject(int ProjectId,int DayProjectId, string DayProjectName,int DayProjectType, string StartTime, string EndTime)
        {
            DayProjectInfo info = null;
            ResultInfo result = new ResultInfo();
            result.IsSuccess = false;
            result.Code = 200;
            result.Msg = "操作失败!";
            bool success = false;
            int code = -1;
            if (DayProjectId > 0)
            {
                info = _projectManageService.GetDayProjectInfoById(DayProjectId);
            }
            else
            {
                info = new DayProjectInfo();
            }
            info.ProjectId = ProjectId;
            info.DayProjectName = DayProjectName;
            info.DayProjectType = DayProjectType;
            info.IsDelete = false;
            info.StartTime = Convert.ToDateTime(StartTime);
            info.EndTime = Convert.ToDateTime(EndTime);
            info.CreateTime = DateTime.Now;
            if (DayProjectId > 0)
            {
                success = _projectManageService.UpdateDayProjectInfo(info);
                if (success)
                {
                    code = 1;
                }
            }
            else
            {
                DayProjectId = _projectManageService.InsertDayProjectInfo(info);
                success = DayProjectId > 0 ? true : false;
                if (success)
                {
                    code = 1;
                }
            }
            if (code == 1)
            {
                result.IsSuccess = true;
                result.Msg = "操作成功!";
            }
            result.Code = code;
            return Json(result);
        }

       /// <summary>
       /// 
       /// </summary>
       /// <param name="dayProjectId"></param>
       /// <param name="userId"></param>
       /// <returns></returns>
        public JsonResult doAssignProject(int dayProjectId, int userId)
        {
            bool IsSuccess = false;
            string msg = "操作失败";
            Hashtable ht = new Hashtable();
            DayProjectInfo dayProjectInfo = _projectManageService.GetDayProjectInfoById(dayProjectId);
            if (dayProjectInfo == null)
            {
                IsSuccess = false;
                 msg = "子计划不存在";
            }
            else
            {
                dayProjectInfo.AssignTime = DateTime.Now;
                dayProjectInfo.UserId = userId;
                dayProjectInfo.StatusType = 5;
                if (_projectManageService.UpdateDayProjectInfo(dayProjectInfo))
                {
                    IsSuccess = true;
                    msg = "分配成功";
                }
                else
                {
                    IsSuccess = false;
                    msg = "分配失败";
                }
            }
            ht["IsSuccess"] = IsSuccess;
            ht["Msg"] = msg;
            return Json(ht);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="dayProjectId"></param>
        /// <param name="userId"></param>
        /// <returns></returns>
        public JsonResult doIssueProject(int dayProjectId, int userId)
        {
            bool IsSuccess = false;
            string msg = "操作失败";
            Hashtable ht = new Hashtable();
            DayProjectInfo dayProjectInfo = _projectManageService.GetDayProjectInfoById(dayProjectId);
            if (dayProjectInfo == null)
            {
                IsSuccess = false;
                msg = "子计划不存在";
            }
            else
            {
                dayProjectInfo.IssueTime = DateTime.Now;
                dayProjectInfo.IsIssue = true;
                dayProjectInfo.StatusType = 10;
                if (_projectManageService.UpdateDayProjectInfo(dayProjectInfo))
                {
                    IsSuccess = true;
                    msg = "下发成功";
                }
                else
                {
                    IsSuccess = false;
                    msg = "下发失败";
                }
            }
            ht["IsSuccess"] = IsSuccess;
            ht["Msg"] = msg;
            return Json(ht);
        }

        #endregion
    }
}
